package com.demo.expertsystemdemo.service.mongodb.springdata;

import com.demo.expertsystemdemo.entity.mongodb.PostInfo;
import com.demo.expertsystemdemo.repository.mongodb.PostInfoRepository;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author lzb22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PostInfoService {
    private final PostInfoRepository postInfoRepository;

    public PostInfoService(@Qualifier(value = "postInfoRepository") PostInfoRepository postInfoRepository) {
        this.postInfoRepository = postInfoRepository;
    }

    public List<PostInfo> list() {
        return postInfoRepository.findAll();
    }

    public PostInfo getOneById(String id) {
        Optional<PostInfo> optional = postInfoRepository.findById(id);
        return optional.get();
    }

    public PostInfo save(PostInfo user) {
        return postInfoRepository.insert(user);
    }

    public void removeById(String id) {
        postInfoRepository.deleteById(id);
    }

    public PostInfo updateById(PostInfo user) {
        return postInfoRepository.save(user);
    }
}
